// ==UserScript==
// @name         文本选中即复制: AutoCopyOnSelect
// @namespace    https://bbs.tampermonkey.net.cn/
// @version      1.0.0
// @description  Automatically copy selected text to clipboard without clearing selection
// @author     ityangs@163.com
// @match      http://*/*
// @match      https://*/*
// @grant        none
// @supportURL https://gitee.com/ityangs/scriptcat-plugins/issues
// @license    GPL License
// @installURL https://gitee.com/ityangs/scriptcat-plugins/src/autoCopyOnSelect
// ==/UserScript==

(function () {
    'use strict';

    // Function to copy text to clipboard using Clipboard API
    function copyToClipboard(text) {
        navigator.clipboard.writeText(text).then(function () {
            console.log('Text copied to clipboard successfully!');
        }, function (err) {
            console.error('Could not copy text: ', err);
        });
    }

    // Function to handle text selection
    function handleSelection() {
        const selectedText = window.getSelection().toString();
        if (selectedText.length > 0) {
            copyToClipboard(selectedText);
        }
    }

    // Add event listener for text selection
    document.addEventListener('mouseup', handleSelection);
    document.addEventListener('keyup', handleSelection);

})();
